package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;

import Communication.Client;
import Communication.MessageHandle;
import Controller.UpdateCustomerController.CustomerInfoListener;
import DB.CustomerHistoryQuery;
import DB.CustomerQuery;
import Entity.Customer;
import Entity.CustomerCard;
import Entity.Worker;
import View.SearchCustomerHistoryView;
import View.UpdateCustomerDeatilsView;
import View.UpdateCustomerHistoryView;
import View.UpdateCustomerView;

/** This class controls and manages the process that allows to search customers for updating their customer card */
public class SearchCustomerHistoryController  implements Serializable {
	private static final long serialVersionUID = 42L;
	/**  Required attributes */
	CustomerHistoryQuery customerHistoryModel;
	static SearchCustomerHistoryView customerHistoryView;
	static UpdateCustomerHistoryView updateCustomerHistoryView;
	CustomerHistoryListener customerHistoryListener;
	Client client;
	UpdateCustomerHistoryController customerHistoryController;
	int role = 0;
	static CustomerCard cust = new CustomerCard();
	static String SaleId ;

	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param handleNewSaleController
	 * @param saleid
	 */
	public SearchCustomerHistoryController(Client client, HandleNewSaleController handleNewSaleController, String saleid) {
		this.client = client;
		customerHistoryView =  new SearchCustomerHistoryView();
		customerHistoryView.showWindow();
		customerHistoryListener = new CustomerHistoryListener(handleNewSaleController);
		getProducts("getProducts");
		updateCustomerHistoryView = new UpdateCustomerHistoryView();
		updateCustomerHistoryView.setLocation(30, 30);
		customerHistoryView.updateCustomerListener(customerHistoryListener );
		customerHistoryView.setLocation(30, 30);
		updateCustomerHistoryView .addActionListenerOur(customerHistoryListener);
		role = client.getCurrentWorker().getRole();
		SaleId=saleid;

	}
	/**
	 * function that gets products to show in combobox
	 * @param messege
	 */
	private void getProducts(String messege) {
		MessageHandle msg = new MessageHandle(null, MessageHandle.GET_PRODUCTS, false);
		msg.setMsg(messege);
		try {
			client.sendToServer(msg);
		} catch (IOException e) {
			System.out.print("SearchCustomerHistoryController->GetProducts");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**  Inner Listener class */ 
	class CustomerHistoryListener implements ActionListener
	{
		HandleNewSaleController handleNewSaleController;

		/**
		 * creats CustomerHistory Listener
		 * @param handleNewSaleController
		 */
		public CustomerHistoryListener(HandleNewSaleController handleNewSaleController){
			this.handleNewSaleController = handleNewSaleController;
			customerHistoryModel = new CustomerHistoryQuery();
			customerHistoryView.showWindow();
			handleNewSaleController.hideWindow();
		}
		@Override
		/**
		 * Handle Actions
		 */
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == customerHistoryView.getSearchBtn() || event.getActionCommand().compareTo("Update")==0 ) {
				if (customerHistoryModel.isEmpty(customerHistoryView.getCUstomer().toString()))
					SystemController
					.displayErrorMessage("ID Field Is Missing!");
				else {

					switch (event.getActionCommand()) {
					case "Search":			//search button pressed button
						cust.setCSaleID(SaleId);
						cust.setCCustomerID((customerHistoryView.getCUstomer()));
						MessageHandle msg = new MessageHandle(cust,
								MessageHandle.UPDATE_GET_CUSTOMER_HISTORY, false);
						msg.setMsg("FindCustomer");
						try {
							client.sendToServer(msg);
						} catch (IOException e) {
							System.out
							.println("UpdateCustomerHistoryController->ActionPerformed");
							e.printStackTrace();
						}

						break;
					case "Update":			//Update button pressed button
						int confirm=SystemController.displayConfirmationMessage("Are You Sure?");
						if(confirm ==0){
							cust.setCProductName(updateCustomerHistoryView.getProduct());
							System.out.println(updateCustomerHistoryView.getCheckApproval());
							cust.setCustApproval(updateCustomerHistoryView.getCheckApproval());
							cust.setCContactCust(updateCustomerHistoryView.getCheckContact());
							cust.setCallTime((updateCustomerHistoryView.getCallTime()));
							cust.setCustResponse(updateCustomerHistoryView.getCustRes());

							MessageHandle msg2 = new MessageHandle(cust,
									MessageHandle.UPDATE_GET_CUSTOMER_HISTORY, false);
							msg2.setMsg("UpdateCustomerCard");
							try {
								client.sendToServer(msg2);
							} catch (IOException e) {
								System.out
								.println("UpdateCustomerHistoryController->ActionPerformed->updatecard");
								e.printStackTrace();
							}
						}
						break;

					case "Back":			//Update button pressed button
						//						workerMainController.showWindow();
						customerHistoryView.hideWindow();
						customerHistoryView.clearFields();
						break;
					}
				}
			}else{
				if (event.getSource() == customerHistoryView.getBackBtn())
				{	
					customerHistoryView.hideWindow();
					handleNewSaleController.showWindow();
				}
				else {
					// workerMainController.showWindow();
					updateCustomerHistoryView.hideWindow();
					customerHistoryView.showWindow();
					customerHistoryView.clearFields();
				}
			}
		}
	}

	/**
	 *  shows window
	 */
	public void showWindow() {
		customerHistoryView.showWindow();		
	}

	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void UpdateView(MessageHandle msg) {
		switch (msg.getMsg()) {

		case "NotExist":
			SystemController.displayErrorMessage("Customer do not exist!");
			break;
		case "Exist":
			CustomerCard card = (CustomerCard) msg.getData();
			customerHistoryView.hideWindow();
			updateCustomerHistoryView.showWindow();
			updateCustomerHistoryView.setDetails(card,cust.getCCustomerID());
			break;
		case "UpdateSuccess":
			CustomerCard cust = (CustomerCard) msg.getData();
			SystemController.displayMessage("Customer Card for customer: "+ cust.getCCustomerID() +"\n have been Updated!!");
			break;
		}
	}
	/**
	 * function that adds customers to the customers list to show in combobox
	 * @param customers
	 */
	public static void addCustomers(String [] customers){
		customerHistoryView.setCustomers(customers);
	}
}

